/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPanelVisualProcessor.java
 *
 * Created on 02.12.2009, 18:30:56
 */

package de.jmda.mproc.swingui;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import de.jmda.gui.swing.mvc.ControllerContextProvider;
import de.jmda.gui.swing.mvc.ControllerSupportEnabled;
import de.jmda.util.fileset.FileSetJavaFilesOnly;
import de.jmda.util.gui.swing.JPanelFavouritesModel;

/**
 *
 */
@ControllerSupportEnabled
public class JPanelVisualProcessor
    extends JPanel
    implements ControllerContextProvider
{
  private static final long serialVersionUID = 1L;

	/** Creates new form JPanelVisualProcessor */
	private JPanelVisualProcessor()
	{
		initComponents();
	}

	/** Creates new form JPanelVisualProcessor */
	public JPanelVisualProcessor(
	    JPanelVisualProcessorModel jPanelVisualProcessorModel)
	{
		this();
		controller.refreshControllerContext();
		controller.setModel(jPanelVisualProcessorModel);
	}
	
	public void launchProcessor()
	{
		controller.launchActionPerformed(
				new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "launch processor"));
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {
		GridBagConstraints gridBagConstraints;

    jPanelFileSetJavaSourceFiles = new JPanel();
    jPanelAnnotationProcessors = new JPanel();
    jScrollPaneAnnotationProcessors = new JScrollPane();
    jListAnnotationProcessors = new JList();
    jPanelButtonBar = new JPanel();
    jButtonAdd = new JButton();
    jButtonRemove = new JButton();
    jButtonLaunch = new JButton();

    setLayout(new GridBagLayout());

    jPanelFileSetJavaSourceFiles.setLayout(new GridBagLayout());
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.fill = GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    add(jPanelFileSetJavaSourceFiles, gridBagConstraints);

    jPanelAnnotationProcessors.setBorder(BorderFactory.createTitledBorder("annotation processors"));
    jPanelAnnotationProcessors.setLayout(new GridBagLayout());

    jListAnnotationProcessors.setModel(new AbstractListModel() {
      String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
      public int getSize() { return strings.length; }
      public Object getElementAt(int i) { return strings[i]; }
    });
    jListAnnotationProcessors.addListSelectionListener(new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent evt) {
        jListAnnotationProcessorsValueChanged(evt);
      }
    });
    jScrollPaneAnnotationProcessors.setViewportView(jListAnnotationProcessors);

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.fill = GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    jPanelAnnotationProcessors.add(jScrollPaneAnnotationProcessors, gridBagConstraints);

    jPanelButtonBar.setLayout(new GridBagLayout());

    jButtonAdd.setText("add");
    jButtonAdd.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        jButtonAddActionPerformed(evt);
      }
    });
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.anchor = GridBagConstraints.NORTH;
    jPanelButtonBar.add(jButtonAdd, gridBagConstraints);

    jButtonRemove.setText("remove");
    jButtonRemove.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        jButtonRemoveActionPerformed(evt);
      }
    });
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.anchor = GridBagConstraints.NORTH;
    jPanelButtonBar.add(jButtonRemove, gridBagConstraints);

    jButtonLaunch.setText("launch");
    jButtonLaunch.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        jButtonLaunchActionPerformed(evt);
      }
    });
    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
    gridBagConstraints.anchor = GridBagConstraints.NORTH;
    jPanelButtonBar.add(jButtonLaunch, gridBagConstraints);

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.fill = GridBagConstraints.VERTICAL;
    jPanelAnnotationProcessors.add(jPanelButtonBar, gridBagConstraints);

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    add(jPanelAnnotationProcessors, gridBagConstraints);
  }// </editor-fold>//GEN-END:initComponents

	private void jButtonAddActionPerformed(ActionEvent evt)//GEN-FIRST:event_jButtonAddActionPerformed
	{//GEN-HEADEREND:event_jButtonAddActionPerformed
		controller.addActionPerformed(evt);
	}//GEN-LAST:event_jButtonAddActionPerformed

	private void jButtonRemoveActionPerformed(ActionEvent evt)//GEN-FIRST:event_jButtonRemoveActionPerformed
	{//GEN-HEADEREND:event_jButtonRemoveActionPerformed
		controller.removeActionPerformed(evt);
	}//GEN-LAST:event_jButtonRemoveActionPerformed

	private void jButtonLaunchActionPerformed(ActionEvent evt)//GEN-FIRST:event_jButtonLaunchActionPerformed
	{//GEN-HEADEREND:event_jButtonLaunchActionPerformed
		controller.launchActionPerformed(evt);
	}//GEN-LAST:event_jButtonLaunchActionPerformed

	private void jListAnnotationProcessorsValueChanged(ListSelectionEvent evt)//GEN-FIRST:event_jListAnnotationProcessorsValueChanged
	{//GEN-HEADEREND:event_jListAnnotationProcessorsValueChanged
		controller.annotationProcessorsValueChanged(evt);
	}//GEN-LAST:event_jListAnnotationProcessorsValueChanged

	/**
	 * @param args the command line arguments
	 */
	public static void main(final String args[])
	{
//		try
//		{
//			UIManager.setLookAndFeel(NimbusLookAndFeel.class.getName());
//		}
//		catch (Exception e)
//		{
			try
			{
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			}
			catch (Exception e1)
			{
				// no exception expected with system look and feel
			}
//		}

		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				JFrame jFrame =
				    new JFrame("test window for "
				        + JPanelVisualProcessor.class.getName());

				jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

				List<File> favourites = new LinkedList<File>();

				try
        {
	        favourites.add(new File(new File(".").getCanonicalPath()));
        }
        catch (IOException e)
        {
        	System.err.println("failure getting canonical path for .");
        }

				List<String> annotationProcessorClassNames = new LinkedList<String>();
				
				jFrame.add(
						new JPanelVisualProcessor(
								new JPanelVisualProcessorModel(
										"test",
										annotationProcessorClassNames,
            				new JPanelFileSetJavaSourceFilesModel(
            						new FileSetJavaFilesOnly(),
            						new JPanelFavouritesModel(favourites)))));

				jFrame.pack();
				jFrame.setLocationRelativeTo(null);
				jFrame.setVisible(true);
			}
		});
	}

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private JButton jButtonAdd;
  private JButton jButtonLaunch;
  private JButton jButtonRemove;
  private JList jListAnnotationProcessors;
  private JPanel jPanelAnnotationProcessors;
  private JPanel jPanelButtonBar;
  private JPanel jPanelFileSetJavaSourceFiles;
  private JScrollPane jScrollPaneAnnotationProcessors;
  // End of variables declaration//GEN-END:variables
	//GEN-BEGIN:controller support
	@de.jmda.gui.swing.mvc.ControllerSupportDisabled                     
	private de.jmda.mproc.swingui.JPanelVisualProcessorController controller =                       
			new de.jmda.mproc.swingui.JPanelVisualProcessorController(this);                             
                                                                         
	public de.jmda.gui.swing.mvc.ControllerContext getControllerContext()
	{                                                                      
		return new de.jmda.mproc.swingui.JPanelVisualProcessorControllerContext(
				this,
				jButtonAdd,
				jButtonLaunch,
				jButtonRemove,
				jListAnnotationProcessors,
				jPanelAnnotationProcessors,
				jPanelButtonBar,
				jPanelFileSetJavaSourceFiles,
				jScrollPaneAnnotationProcessors);                             
	}                                                                      
	//GEN-END:controller support
}